package com.company.planner.web.screens.ikanalysisrule;

import com.company.planner.entity.*;
import com.company.planner.service.EsService;
import com.haulmont.cuba.gui.components.HasValue;
import com.haulmont.cuba.gui.components.TwinColumn;
import com.haulmont.cuba.gui.components.ValidationErrors;
import com.haulmont.cuba.gui.screen.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@UiController("planner_IkAnalysisRule.edit")
@UiDescriptor("ik-analysis-rule-edit.xml")
@EditedEntityContainer("ikAnalysisRuleDc")
@LoadDataBeforeShow
public class IkAnalysisRuleEdit extends StandardEditor<IkAnalysisRule> {
    @Inject
    private Logger log;
    @Inject
    private TwinColumn twinColumn_1;

    private StringBuffer ruleExpression = new StringBuffer("");
    @Inject
    private TwinColumn<Indexs> twinColumn;
    @Inject
    private EsService esService;

    @Subscribe
    public void onInitEntity(InitEntityEvent<IkAnalysisRule> event) {
        event.getEntity().setActive(ActiveEnum.YES);
    }

    @Subscribe("twinColumn")
    public void onTwinColumnValueChange(HasValue.ValueChangeEvent<Collection<Indexs>> event) {
        Collection<Indexs> value = event.getValue();
        if (value != null && !value.isEmpty()) {
            if (value.size() == 1) {
                twinColumn.setEditable(false);
            }
            for (Indexs indexs : value) {
                log.info("event.getValue() : {}", indexs.getIndexName());
                getEditedEntity().setIndexs(indexs);
                getEditedEntity().setIndexName(indexs.getIndexName());
                ruleExpression.append(indexs.getIndexName()).append(" in (");
                List<Mappings> mappingList = indexs.getMappingList();
                List<String> mappingNames = mappingList.stream()
                        .filter(mappings -> !mappings.getLevel().equals(NestedLevel.ZERO))
                        .map(Mappings::getMappingName)
                        .collect(Collectors.toList());
                twinColumn_1.setOptionsList(mappingNames);
            }
        }
    }

    @Subscribe("twinColumn_1")
    public void onTwinColumn_1ValueChange(HasValue.ValueChangeEvent event) {
        Collection<String> mappingNames = (Collection<String>) event.getValue();
        List<String> names = new ArrayList<>();
        if (mappingNames != null && !mappingNames.isEmpty()) {
            names.add(mappingNames.toString());
        }
        String name = names.stream().map(s -> s).collect(Collectors.joining(", "));
        name = StringUtils.strip(name, "[]");
        getEditedEntity().setRuleExpression(ruleExpression.toString() + name + ")");
        getEditedEntity().setMappingName(name);
        names.clear();
    }

    @Subscribe
    public void onValidation(ValidationEvent event) {
        IkAnalysisRule ikAnalysisRule = getEditedEntity();
        boolean b = esService.existIkAnalysisRule(ikAnalysisRule.getIndexs());
        if (b) {
            event.addErrors(ValidationErrors.of("索引：" + ikAnalysisRule.getIndexs().getIndexName() + "已存在配置的规则"));
        }
    }


}